package com.syqc.monitor.ui.sms

import android.widget.ImageView
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.loadGif
import com.syqc.entity.SmsCmdInfo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivitySmsCmd2Binding
import com.syqc.monitor.popup.SMSCmdPopup

class SmsCmdActivity :
    BaseBarActivity<ActivitySmsCmd2Binding>(ActivitySmsCmd2Binding::inflate) {
    private val mViewModel by viewModels<SMSCmdVieModel>()
    private val mCmdPopup by lazy { SMSCmdPopup(this) }
    private val mAdapter by lazy {
        SmsCmdAdapter { name, child ->
            mCmdPopup.showAtLocation(window.decorView, "$name---${child.name}", child.children)
        }
    }

    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.rvTmnCs,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(this, R.mipmap.ic_empty)

        view
    }

    //是否在展示收藏
    private var isShowCollect = false


    private var isSearch = false

    override fun initView() {
        ToolbarHelper.setToolbar(this, R.string.title_other_dictate)
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
        binding.rvTmnCs.layoutManager = LinearLayoutManager(this)
        binding.rvTmnCs.adapter = mAdapter
        initData()
    }

    private fun initData() {
        mViewModel.getSMSCmd()
        mViewModel.smsObserver().autoDispose(scopeProvider).subscribe {
            if (it.isNullOrEmpty()) {
                mAdapter.setEmptyView(emptyView)
            }
            mAdapter.setList(it)
        }
        mViewModel.collectNumLive.observe(this) {
            binding.rbCollect.text = getString(R.string.desc_had_collect, it.size)
            if (!isSearch&&isShowCollect) {
                if (it.isNullOrEmpty()) {
                    mAdapter.setEmptyView(emptyView)
                }
                mAdapter.setList(it)
            }
        }
    }


    override fun setListener() {
        binding.editSearch.doAfterChanged {
            binding.ivClear.isInvisible = it.isNullOrEmpty()
            if (it.isNullOrEmpty()) {
                mAdapter.isShowChild = true
                binding.rgChange.isVisible = true
                mViewModel.setAllSms()
            } else {
                isSearch = true
                mAdapter.isShowChild = false
                binding.rgChange.isVisible = false
                mViewModel.getLikeDAta(it.toString())
            }
        }

        binding.ivClear.setOnClickListener {
            binding.editSearch.setText("")
            mAdapter.isShowChild = true
            isSearch = false
            binding.rgChange.isVisible = true
            if (isShowCollect){
                val collects = mViewModel.getCollects()
                if (collects.isEmpty()) {
                    mAdapter.setEmptyView(emptyView)
                }
                mAdapter.setList(collects)
                isShowCollect = true
            }
        }

        binding.rgChange.setOnCheckedChangeListener { _, checkedId ->
            if (checkedId == R.id.rbAll) {
                val allTmn = mViewModel.getAllTmn()
                mAdapter.setList(allTmn)
                if (allTmn.isNullOrEmpty()) {
                    mAdapter.setEmptyView(emptyView)
                }
                isShowCollect = false
            } else {
                val collects = mViewModel.getCollects()
                if (collects.isEmpty()) {
                    mAdapter.setEmptyView(emptyView)
                }
                mAdapter.setList(collects)
                isShowCollect = true
            }
        }
        mAdapter.setOnItemChildClickListener { _, _, position ->
            val cmdInfo = mAdapter.data[position]
            cmdInfo.isChoose = !cmdInfo.isChoose
            mViewModel.setCollect(cmdInfo.isChoose, cmdInfo)
            mAdapter.notifyDataSetChanged()
        }
    }
}

class SmsCmdAdapter(private val listener: (String, SmsCmdInfo.Children) -> Unit) :
    BaseQuickAdapter<SmsCmdInfo, BaseViewHolder>(R.layout.item_sms_cmd) {
    var isShowChild = true
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    init {
        addChildClickViewIds(R.id.tvTmn)
    }

    override fun convert(holder: BaseViewHolder, item: SmsCmdInfo) {
        item.run {
            val tvTmn = holder.getView<TextView>(R.id.tvTmn)
            tvTmn.isSelected = isChoose
            tvTmn.text = name
            val rvTmn = holder.getView<RecyclerView>(R.id.rvTmn)
            holder.setGone(R.id.lineTmn, isShowChild)
            rvTmn.isVisible = isShowChild

            val adapter = SmsChildAdapter()
            rvTmn.adapter = adapter
            adapter.setList(children)
            rvTmn.layoutManager = GridLayoutManager(context, 3)
            adapter.setOnItemClickListener { adapter, view, position ->
                listener.invoke(item.name, item.children[position])
            }
        }
    }
}

class SmsChildAdapter : BaseQuickAdapter<SmsCmdInfo.Children, BaseViewHolder>(R.layout.item_tmn) {
    override fun convert(holder: BaseViewHolder, item: SmsCmdInfo.Children) {
        holder.setText(R.id.tvTmnChild, item.name)
    }

}